%% Load the data - Select the File: 'ViscosityProfiles\0pt5PEO.mat'
close all; clear all; clc;
load('.\ViscosityProfiles\0pt5PEO.mat')

vis = []; vis = cell2mat(mu');
xlim_max = 1000; x = []; x = xPos + (500 - median(xPos));

FitViscSlope = [];
first = 4; last = 8;
for i = 1:length(mu)
    clear Mu_Exp p
    Mu_Exp = flip(mu{1,i}(first:last))
    p = polyfit(xPos(first:last),Mu_Exp,1)
    FitViscSlope(i,1) = p(1);FitViscSlope(i,2) = p(2);
    FitVisc = polyval(FitViscSlope(i,:),xPos(first:last));
end
Grad_Eta = mean(FitViscSlope(4:10,1))

st = 1, fin = 10;
LinVisProfile = figure('color',[1 1 1])
    col = 256*length(st:fin);
    cmapdef = colormap(jet(256*length(st:fin)-255)); %Define Colormap
    col_int = [256 linspace(256,col-256,length(st:fin)-1)+256]-255;
    colmap = cmapdef(col_int, :);
    for i = 1:length(st:fin)
        plot(x,vis(i,:),'color',colmap(i,:));
        hold on;
    end
    hold off;

    ax = gca;
    ax.FontSize = 7;
    xlim([0 xlim_max]);ylim([0 4.5]); xticks([0 250 500 750]); yticks([0,1,2,3,4,5]);
    set(gca, 'FontName', 'Arial')
    xlabel('Position x (\mum)','FontSize',8);ylabel('Viscosity \eta (cP)','FontSize',8);
    % Color Bar
    cbar_label = (st:fin)*interval - interval;
    colormap(jet);
        h = colorbar('color',[0 0 0]);
        h.Ticks = linspace(0,1,length(st:fin));
        h.LineWidth = 1
        h.TickLabels = num2cell(cbar_label);
        h.FontSize = 7;
        h.FontName = 'Arial'; h.Location = 'eastoutside';
        %title(h,'Time (min)')
      
    % Crop Figure
    alw = 1; % Border line width
    afs = 7; % Font Size
    xlabel(''); ylabel('');
    mypapersize = [3.9 2.9]; % [width height]
    set(gca, 'FontName', 'Arial');
    set(gca,'LineWidth',alw);
    set(gca,'XColor',[0 0 0]);set(gca,'YColor',[0 0 0]);
    set(gcf, 'PaperUnits', 'inches', 'papersize', mypapersize, 'PaperPosition', [0 0 mypapersize]);
    set(LinVisProfile,'Units','inches','Position',[0 0 mypapersize]);
%% Save Figure
figdir = '.\ExtendedDataFigure1';
print(LinVisProfile, '-painters','-dpdf', '-r1200', [figdir,'\5cP_TransViscProfile.pdf']);

%% Save Data
tab = 1;
dir_Excel = '.\Source data\ExtendedData\';
writematrix(x,[dir_Excel,'ExtDataFigure1.xlsx'],'Sheet',tab,'Range','C1')
writematrix(vis(st:fin,:),[dir_Excel,'ExtDataFigure1.xlsx'],'Sheet',tab,'Range','C3')